<template>
  <div class="ChatPanel">
    <div class="tokenNum">{{ tokenNum }}/{{ allTokenNum }}</div>
    <div class="chatPanel--content">
      <c-input v-model:value="content" class="ChatInput" round placeholder="请输入发送的信息" type="textarea" size="small"
        :autosize="{
      minRows: 3,
      maxRows: 6
    }" />
      <div v-if="TauriF.isTauri()" class="voiceButton">
        <c-button v-if="!isStart" size="small" quaternary type='primary' @click="start"
          iconName="KeyboardVoiceOutlined" />
        <c-button class="animateVoiceShadow" type='primary' ghost v-else size="small" circle @click="stop"
          iconName="KeyboardVoiceFilled" />
      </div>
      <c-button size="small" quaternary type='primary' @click="appendMessagge(content)" iconName="SendRound"
        class="sendButton" />
      <SelectAction @close="isStarthowSelectAction = false" v-if="isStarthowSelectAction" />
    </div>
  </div>
</template>

<script setup lang='ts'>
const TauriF = Tauri
const isStarthowSelectAction = ref(false)
const { appendMessagge, content } = useMessages()
const { chatMap, currentChat } = storeToRefs(chatStore())
const allContent = computed(() => {
  return ObjectToArray<Message>(chatMap.value.get(currentChat.value)?.messages!)!.reduce((pre, cur) => pre + cur.content, '')
})
const { start, stop, isStart } = useVoice({
  lang: 'zh-CN',
  continuous: true, onresult: ({ results }: { results: SpeechRecognitionResultList }) => {
    const message = results.item(results.length - 1)[0].transcript
    appendMessagge(message)
  }
})
const { getSetValue } = chatSettingStore()
const apiConfig = getSetValue('api')
const { tokenNum } = useTikToken(content, apiConfig('modelName'))
const { tokenNum: allTokenNum } = useTikToken(allContent, apiConfig('modelName'))
const showSelectAction = () => {
  isStarthowSelectAction.value = true
}
defineExpose({
  showSelectAction
})
</script>

<style lang="scss">
@media screen and (max-width: 800px) {
  .chatPanel--content {
    bottom: 10px !important;
  }
}

@keyframes shadow-shrink {
  0% {
    box-shadow: 0 0 10px 0 #18a058;
  }

  100% {
    box-shadow: 0 0 1px 0 #18a058;
  }
}


.animateVoiceShadow {
  box-shadow: 0 0 10px 0 #18a058;
  animation: shadow-shrink .5s infinite alternate;
}

@media screen and (min-width: 800px) {
  .ChatPanel {
    width: 60% !important;
  }
}

.ChatPanel {
  width: 90%;
  margin: 0 auto;
  display: flex;
  position: relative;
  padding-bottom: 15px;
  align-items: flex-end;

  .chatPanel--content {
    .n-input {
      padding-bottom: 16px
    }

    display: flex;
    justify-content: center;
    width: 100%;

    .ChatInput {
      min-height: 50px;
      border-radius: 6px;
    }

    .n-input-wrapper {
      padding: 10px !important;
    }

    .sendButton {
      position: absolute;
      right: 10px;
      bottom: 23px;
    }

    .voiceButton {
      position: absolute;
      right: 10px;
      top: 8px;
      width: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 40x;
    }
  }

}

.tokenNum {
  font-size: 12px;
  color: #999;
  margin-right: 12px;
  position: absolute;
  z-index: 2;
  left: 10px;
  bottom: 20px;
}
</style>